package com.hopes.bizz.enforcement.integral.listener.impl;

import cn.hutool.core.collection.CollUtil;
import com.hopes.bizz.enforcement.common.constant.IntegralEventEnum;
import com.hopes.bizz.enforcement.hid.entity.RiskHidHiddenDangerEntity;
import com.hopes.bizz.enforcement.hid.service.RiskHidHiddenDangerService;
import com.hopes.bizz.enforcement.integral.bo.IntegralComputeBO;
import com.hopes.bizz.enforcement.integral.bo.IntegralEvent;
import com.hopes.bizz.enforcement.integral.entity.RiskIntegralRecordEntity;
import com.hopes.bizz.enforcement.integral.listener.IntegralEventSupport;
import com.hopes.bizz.enforcement.integral.service.EnfIntegralEventService;
import com.hopes.bizz.enforcement.integral.service.RiskIntegralRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author yumr
 * @ProjectName bizz
 * @Description 隐患积分事件
 * @time 2023/8/20 22:39
 */
@Slf4j
@Component("hid_report_integral")
public class HidReportEventHandler extends IntegralEventSupport {

    private final RiskHidHiddenDangerService riskHidHiddenDangerService;

    protected HidReportEventHandler(EnfIntegralEventService enfIntegralEventService,
                                    RiskIntegralRecordService riskIntegralRecordService, RiskHidHiddenDangerService riskHidHiddenDangerService) {
        super(enfIntegralEventService, riskIntegralRecordService);
        this.riskHidHiddenDangerService = riskHidHiddenDangerService;
    }

    @Override
    public void handler(IntegralEvent integralEvent) {
        // 1、业务Id
        final Long businessId = Optional.of(integralEvent).map(IntegralEvent::getBusinessId).orElse(null);
        final RiskHidHiddenDangerEntity riskHidHiddenDanger = riskHidHiddenDangerService.getById(businessId);
        if (Objects.isNull(riskHidHiddenDanger)) {
            log.error("隐患排查任务数据不存在，业务标识：{}", businessId);
            return;
        }
        // 2、个人上报隐患积分计算
        final List<RiskIntegralRecordEntity> list = new ArrayList<>();
        final IntegralComputeBO reportIntegralComputeBO = new IntegralComputeBO(IntegralEventEnum.hid_report_integral, riskHidHiddenDanger, integralEventMap());
        final List<RiskIntegralRecordEntity> riskReportIntegralRecordList = reportIntegralComputeBO.computeIntegral();
        if (CollUtil.isNotEmpty(riskReportIntegralRecordList)) {
            list.addAll(riskReportIntegralRecordList);
        }
        final boolean b = saveIntegralRecordBatch(list);
        log.debug("隐患上报积分事件处理结果：{}", b);
    }
}
